import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:seedfour/app/constants.dart';
import 'package:seedfour/app/views/module_notice/notice_detail/widget/bottom_bar.dart';
import 'package:seedfour/app/views/module_notice/notice_detail/widget/history_info.dart';
import 'package:seedfour/app/views/module_notice/notice_detail/widget/notice_info.dart';
import 'package:seedfour/app/views/module_notice/notice_detail/widget/receive_info.dart';

import 'logic.dart';

class NoticeDetailPage extends StatelessWidget {
  NoticeDetailPage({Key? key}) : super(key: key);

  final logic = Get.find<NoticeDetailLogic>();
  final state = Get.find<NoticeDetailLogic>().state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('案源详情'),
        leading: IconButton(
          icon: Icon(
            Icons.arrow_back_ios,
            size: 16.w,
          ),
          onPressed: () {
            Get.back();
          },
        ),
        actions: [
          Obx(
            () => (state.noticeData.value.status == 1 &&
                    state.noticeData.value.receiveUid == state.ownerUid.value)
                ? MenuAnchor(
                    alignmentOffset: Offset(-40.w, 0),
                    builder: (context, controller, child) {
                      return IconButton(
                          onPressed: () {
                            if (controller.isOpen) {
                              controller.close();
                            } else {
                              controller.open();
                            }
                          },
                          icon: Icon(Icons.more_horiz, size: 24.w));
                    },
                    menuChildren: [
                      MenuItemButton(
                        onPressed: () => logic.dealNotice(),
                        child: const Text('成交案源'),
                      ),
                      MenuItemButton(
                        onPressed: () => logic.toReportReturn(),
                        child: const Text('退回案源'),
                      ),
                    ],
                    style: const MenuStyle(
                      backgroundColor: WidgetStatePropertyAll(Colors.white),
                      elevation: WidgetStatePropertyAll(0),
                      padding: WidgetStatePropertyAll(EdgeInsets.zero),
                      side: WidgetStatePropertyAll(
                        BorderSide(color: Color(0xFFE3E6EB), width: 0.5),
                      ),
                    ),
                  )
                : Container(),
          ),
        ],
      ),
      body: Obx(() {
        if (state.isLoading.value) {
          return const Center(child: CircularProgressIndicator());
        } else {
          return ListView(
            children: [
              SizedBox(height: kSpaceMedium),
              NoticeInfoWidget(
                data: state,
                onLongPress: (content) {
                  Clipboard.setData(ClipboardData(text: content));
                  Get.snackbar('提示', '案源描述已复制到剪贴板');
                },
              ),
              SizedBox(height: kSpaceMedium),
              ReceiveInfoWidget(
                data: state,
                onClickCase: (caseId) => logic.toCaseDetail(caseId),
              ),
              SizedBox(height: kSpaceMedium),
              HistoryInfoWidget(data: state),
              SizedBox(height: kSpaceMedium),
            ],
          );
        }
      }),
      bottomNavigationBar: BottomBarWidget(
        data: state,
        onClickProgress: () => logic.toReportProgress(),
        onClickReturn: () => logic.toReportReturn(),
        onClickReceive: () => logic.receiveNotice(),
        onClickDeal: () => logic.dealNotice(),
      ),
    );
  }
}
